﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.View;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_VehicleBusiness : BaseBusiness<PB_Vehicle>, IPB_VehicleBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_VehicleBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_VehicleDTO>> GetDataListAsync(PageInput<PB_VehicleConditonDTO> input)
        {
            var search = input.Search;

            Expression<Func<PB_Vehicle, PB_Supplier, PB_VehicleDTO>> select = (a, b) => new PB_VehicleDTO
            {
                SupplierName = b.Name,
            };

            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_Supplier>() on a.SupplierId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()

                    select @select.Invoke(a, b);
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                q = q.Where(m => m.SupplierId.Contains(search.Keyword));
            }
            if (search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                q = q.Where(m => m.DriverName.Contains(search.Keyword) || m.PlateNO.Contains(search.Keyword));
            }
            if (!search.PlateNO.IsNullOrEmpty())
            {
                q = q.Where(m => m.PlateNO.Contains(search.PlateNO));
            }
            //q = q.Where(x => _operator.Property.DeptIds.Contains(x.DeptId));
            return await q.GetPageResultAsync(input);



            //var q = GetIQueryable();
            //var where = LinqHelper.True<PB_Vehicle>();
            //var search = input.Search;

            ////筛选
            //if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<PB_Vehicle, bool>(
            //        ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
            //    where = where.And(newWhere);
            //}

            //return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_Vehicle> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }
        /// <summary>
        /// 根据货运公司ID查找车牌信息
        /// </summary>
        /// <param name="SupplierId"></param>
        /// <returns></returns>
        public async Task<PageResult<PB_Vehicle>> GetTheDatabySupplier(string SupplierId)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Vehicle>();

            //筛选
            if (!SupplierId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_Vehicle, bool>(
                    ParsingConfig.Default, false, $@"SupplierId.Contains(@0)", SupplierId);
                where = where.And(newWhere);
            }
            q = q.Where(x => _operator.Property.DeptIds.Contains(x.DeptId));
            return await q.Where(where).GetPageResultAsync(new PageInput());
        }

        [DataRepeatValidate(new string[] { "PlateNO" }, new string[] { "车牌号" })]
        public async Task AddDataAsync(PB_Vehicle data)
        {
            string ID = IdHelper.GetId();
            data.Id = ID;
            data.CreatorId = _operator.UserId;
            data.DeptId = _operator?.Property?.DepartmentId;
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_Vehicle data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            var Vehicle =await Db.GetIQueryable<PB_TransportBill>().Where(m => ids.Contains(m.Vehicle)).ToListAsync();
            if (Vehicle.Count>0)
            {
                throw new BusException("货运派车有引用的车辆!");

            }
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}